/*************************************************************************
 * This file is part of HeroesForge.
 *
 * HeroesForge is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * HeroesForge is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with HeroesForge.  If not, see <http://www.gnu.org/licenses/>.
 *************************************************************************/
package org.diskordia.heroesforge.console.utils;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.Window;

/**
 * Several utilities for the GUI components.
 * @author FSuite
 */
public class GuiUtils {
	/**
	   * Centers <CODE>wind</CODE> within its parent. If it has no parent then
	   * center within the screen. If centering would cause the title bar to go
	   * above the parent (I.E. cannot see the titlebar and so cannot move the
	   * window) then move the window down.
	   * 
	   * @param wind
	   *          The Window to be centered.
	   * 
	   * @throws IllegalArgumentException
	   *           If <TT>wind</TT> is <TT>null</TT>.
	   */
	  public static void centerWithinParent(Window wind) {
	    if (wind == null) {
	      throw new IllegalArgumentException("null Window passed");
	    }
	    
	    final Container parent = wind.getParent();
	    if (parent != null && parent.isVisible()) {
	      center(wind, new Rectangle(parent.getLocationOnScreen(), parent.getSize()));
	    } else {
	      centerWithinScreen(wind);
	    }
	  }
	  
	  /**
	   * Centers <CODE>wind</CODE> within the screen. If centering would cause the
	   * title bar to go off the top of the screen then move the window down.
	   * 
	   * @param wind
	   *          The Window to be centered.
	   * 
	   * @throws IllegalArgumentException
	   *           If <TT>wind</TT> is <TT>null</TT>.
	   */
	  public static void centerWithinScreen(Window wind) {
	    if (wind == null) {
	      throw new IllegalArgumentException("null Window passed");
	    }
	    final Toolkit toolKit = Toolkit.getDefaultToolkit();
	    final Rectangle rcScreen = new Rectangle(toolKit.getScreenSize());
	    final Dimension windSize = wind.getSize();
	    final Dimension parentSize = new Dimension(rcScreen.width, rcScreen.height);
	    if (windSize.height > parentSize.height) {
	      windSize.height = parentSize.height;
	    }
	    if (windSize.width > parentSize.width) {
	      windSize.width = parentSize.width;
	    }
	    center(wind, rcScreen);
	  }
	  
	  /**
	   * Centers <CODE>wind</CODE> within the passed rectangle.
	   * 
	   * @param wind
	   *          The Window to be centered.
	   * @param rect
	   *          The rectangle (in screen coords) to center <CODE>wind</CODE>
	   *          within.
	   * 
	   * @throws IllegalArgumentException
	   *           If <TT>Window</TT> or <TT>Rectangle</TT> is <TT>null</TT>.
	   */
	  private static void center(Component wind, Rectangle rect) {
	    if (wind == null || rect == null) {
	      throw new IllegalArgumentException("null Window or Rectangle passed");
	    }
	    Dimension windSize = wind.getSize();
	    int x = ((rect.width - windSize.width) / 2) + rect.x;
	    int y = ((rect.height - windSize.height) / 2) + rect.y;
	    if (y < rect.y) {
	      y = rect.y;
	    }
	    
	    wind.setLocation(x, y);
	  }
}
